package com.zhang.dao.impl;

import com.zhang.dao.BookingDao;
import com.zhang.pojo.Booking;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookingDaoImpl extends BaseDao implements BookingDao {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    @Override
    //全查
    public List<Booking> selectBooking() {

        try {
            conn = getConnection();
            String sql = "SELECT * FROM booking  ORDER BY bookingDate ASC";
            pstm =conn.prepareStatement(sql);
            rs=pstm.executeQuery();
           List<Booking> bookingList = new ArrayList<Booking>();
           while (rs.next()){
               Booking booking = new Booking();
               booking.setId(rs.getInt(1));
               booking.setUserName(rs.getString(2));
               booking.setCardId(rs.getString(3));
               booking.setStatus(rs.getInt(4));
               booking.setBookingDate(rs.getDate(5));
               bookingList.add(booking);
           }
           return bookingList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Booking> selectBookingGetCardId(String cardId) {
        try {
            conn = getConnection();
            String sql = "SELECT * FROM booking where cardId= ? ";
            pstm =conn.prepareStatement(sql);
            pstm.setObject(1,cardId);
            rs=pstm.executeQuery();
            List<Booking> bookingList = new ArrayList<Booking>();
            while (rs.next()){
                Booking booking = new Booking();
                booking.setId(rs.getInt(1));
                booking.setUserName(rs.getString(2));
                booking.setCardId(rs.getString(3));
                booking.setStatus(rs.getInt(4));
                booking.setBookingDate(rs.getDate(5));
                bookingList.add(booking);

            }
            return bookingList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public int insertBooking(Booking booking) {
        String sql =" INSERT INTO booking (userName,cardID,status,bookingDate)VALUES(?,?,0,?)";
        Object[] obj ={
                booking.getUserName(),
                booking.getCardId(),
                booking.getBookingDate()
        };
        return executeUpdate(sql,obj);
    }

    @Override
    public int updateBooking(int id) {
        String sql =" UPDATE booking SET status = 1 WHERE id = ?";
        Object[] obj ={
                id
        };
        return executeUpdate(sql,obj);

    }

    @Override
    public int count(String cordId) {
        try {
            conn = getConnection();
            String sql ="select count(*) from booking where cardId=?";
            pstm = conn.prepareStatement(sql);
            pstm.setObject(1,cordId);
            rs=pstm.executeQuery();
            if (rs.next()){
                return rs.getInt(1);
            }else {
                return 0;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(rs,pstm,conn);
        }
        return 0;

    }
}
